#include <ros/ros.h>
#include "executor/chassis_executor.h"
#include "executor/gimbal_executor.h"
#include "behavior_tree/behavior_node.h"
#include "behavior_tree/behavior_tree.h"

//#include "example_behavior/aim_behavior.h"
#include "example_behavior/attack_behavior.h"
#include "example_behavior/back_boot_area_behavior.h"//go 0 0 0
#include "example_behavior/bfs_search.h"
#include "example_behavior/blood_behavior.h"
#include "example_behavior/buff_behavior.h"
#include "example_behavior/chase_behavior.h"
#include "example_behavior/circle_behavior.h"
#include "example_behavior/dance_behavior.h"
#include "example_behavior/escape_behavior.h"
#include "example_behavior/gimbal_aim_behavior.h"
#include "example_behavior/gimbalget0_behavior.h"
#include "example_behavior/goal_behavior.h"
#include "example_behavior/gogoal_behavior.h"
#include "example_behavior/patrol_behavior.h"
#include "example_behavior/recovery.h"
#include "example_behavior/search_behavior.h"
#include "example_behavior/shake_behavior.h"
#include "example_behavior/under_attack_behavior.h"
#include "example_behavior/wait_behavior.h"
#include <iostream>
#include "roborts_msgs/FricWhl.h"

void param_init(ros::NodeHandle& p){}

int main(int argc, char **argv){
    ros::init(argc, argv, "behavior_test_node");
	std::string full_path = ros::package::getPath("roborts_decision") + "/config/decision.prototxt";
	ros::NodeHandle nh_p("~") ;
	param_init(nh_p);
    auto blackboard_ptr_ = std::make_shared<roborts_decision::Blackboard>(full_path);
    auto chassis_executor_ptr_ = std::make_shared<roborts_decision::ChassisExecutor>();
    auto gimbal_executor_ptr_ = std::make_shared<roborts_decision::GimbalExecutor>();

    auto circle_action_ = std::make_shared<roborts_decision::CircleAction>(blackboard_ptr_,chassis_executor_ptr_);
    auto chase_action_=std::make_shared<roborts_decision::ChaseAction>(blackboard_ptr_,chassis_executor_ptr_);
    auto dance_action_=std::make_shared<roborts_decision::DanceAction>(blackboard_ptr_, chassis_executor_ptr_, gimbal_executor_ptr_);
    auto shake_action_=std::make_shared<roborts_decision::ShakeAction>(blackboard_ptr_, gimbal_executor_ptr_);
        auto attack_action_=std::make_shared<roborts_decision::AttackAction>(blackboard_ptr_);
        roborts_decision::Blackboard* blackboard = &*blackboard_ptr_;
	roborts_decision::BfsSearch* bfs_search = new roborts_decision::BfsSearch(blackboard);
  chassis_executor_ptr_->Set_Search(bfs_search);

    auto dance_parallel_=std::make_shared<roborts_decision::ParallelNode>("dance parallel", blackboard_ptr_, 1);
    dance_parallel_->AddChildren(dance_action_);
    dance_parallel_->AddChildren(attack_action_);

    auto condition_=std::make_shared<roborts_decision::PreconditionNode>("Condition", blackboard_ptr_, [&]()
    {
        if(blackboard_ptr_->chase_condition())return true;
        else return false;
    }, roborts_decision::AbortType::BOTH);
    condition_->SetChild(chase_action_);

    auto condition2_=std::make_shared<roborts_decision::PreconditionNode>("Condition2", blackboard_ptr_, [&]()
    {
        if(blackboard_ptr_->GetIfEnemy())return true;
        else return false;
    }, roborts_decision::AbortType::BOTH);
    condition2_->SetChild(dance_parallel_);

    auto root_selector_ = std::make_shared<roborts_decision::SelectorNode>("root",blackboard_ptr_);
    root_selector_->AddChildren(condition_);
    root_selector_->AddChildren(condition2_);
    root_selector_->AddChildren(shake_action_);
    roborts_decision::BehaviorTree root(root_selector_,10);
   /* ros::service::waitForService("cmd_fric_wheel");
    ros::NodeHandle n;
    ros::ServiceClient client = n.serviceClient<roborts_msgs::FricWhl>("cmd_fric_wheel");
    roborts_msgs::FricWhl Fric;
    Fric.request.open = true;
    if(!client.call(Fric))
        ROS_ERROR("FRICWHEEL NOT OPEN");
    sleep(3);*/
    root.Run();
    return 0;
}
